import 'whatwg-fetch';
import {o1} from './origin'
const qs = require('querystring');
function Fetch(){}

const postDefaultHeaders = {
    'Content-Type': "application/x-www-form-urlencoded"
}

let fetch = window.fetch;
let prototype = Fetch.prototype;
Fetch.prototype = {
    ...prototype,
    getJson(url,data,callback){
        let _data = typeof data != 'function' ? data : {}
        let _callback = typeof data == 'function' ? data : callback
        fetch(o1 + url)
        .then(res => res.json())
        .then(res => _callback(null,res))
        .catch(err => _callback(err))
    },
    post(url,data,callback,headers){
        fetch(o1 + url, {
            method: 'POST',
            headers: {
                ...postDefaultHeaders,
                ...(headers ? headers : {})
            },
            body: qs.stringify(data)
        })
        .then(res => res.json())
        .then(res => callback(null,res))
        .catch(err => callback(err)) 
    },
    postJson(){

    }
}


export default new Fetch()